package com.baozhun.mall.common.widget.customview.filter

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.baozhun.mall.common.R
import com.baozhun.mall.common.databinding.WidgetCommonFilterViewBinding
import com.baozhun.mall.common.model.bean.FilterItem
import com.baozhun.mall.common.widget.dialog.BaseBottomPopupView
import com.blankj.utilcode.util.ScreenUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BottomPopupView

/**
 * @Title：
 * @Description：
 * @Author：xing.luo
 * @Since：2022/11/12
 * @Version：1.0.0
 */
@SuppressLint("ViewConstructor")
class CustomFilterDialog(context: Context, val items: MutableList<FilterItem>?, private val filterParams: Map<String, String>) : BaseBottomPopupView<WidgetCommonFilterViewBinding>(context) {
    var listener: View.OnClickListener? = null
    var optionViews: MutableList<FilterItemView> = mutableListOf()

    override fun getImplLayoutId(): Int {
        return R.layout.widget_common_filter_view
    }

    override fun onCreate() {
        super.onCreate()
        initView(context)
        initData()
    }

    private fun initView(context: Context) {

    }

    override fun getMaxHeight(): Int {
        return ScreenUtils.getScreenHeight() * 3 / 4
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v?.id) {
            R.id.reset -> {
                for (itemView in this.optionViews) {
                    itemView.reset()
                }
            }
        }
        listener?.onClick(v)
    }

    fun setOnClickCallback(listener: View.OnClickListener) {
        this.listener = listener
    }

    fun initData() {
        if (filterParams.containsKey("price_start")) {
            mBinding.customPriceMin.setText(filterParams["price_start"])
        }
        if (filterParams.containsKey("price_end")) {
            mBinding.customPriceMax.setText(filterParams["price_end"])
        }
        mBinding.filterItemContainer.removeAllViews()
        if (items == null) return
        for (item in this.items) {
            var itemView = FilterItemView(context)
            itemView.setData(item, filterParams)
            optionViews.add(itemView)
            mBinding.filterItemContainer.addView(itemView)
        }
    }

    fun getResult(): HashMap<String, String> {
        val temp = HashMap<String, ArrayList<String>>()
        for (view in optionViews) {
            val pair = view.getResult()
            if (pair.first.isNotEmpty()) {
                val key = pair.first
                val selectList = pair.second
                if (selectList.isNotEmpty()) {
                    if (temp.containsKey(key)) {
                        temp[key]?.addAll(selectList)
                    } else {
                        temp[key] = arrayListOf()
                        temp[key]?.addAll(selectList)
                    }
                }
            }
            view.reset()
        }
        val result = HashMap<String, String>()
        for (key in temp.keys) {
            result.put(key, listToString(temp[key]!!))
        }
        if (mBinding.customPriceMin.text?.isNotEmpty() == true) {
            result["price_start"] = mBinding.customPriceMin.text.toString()
        }
        if (mBinding.customPriceMax.text?.isNotEmpty() == true) {
            result["price_end"] = mBinding.customPriceMax.text.toString()
        }
        return result
    }

    private fun listToString(list: ArrayList<String>): String {
        val buffer = StringBuffer()
        for (value in list) {
            buffer.append(value).append(",")
        }
        if (buffer.isNotEmpty()) {
            buffer.deleteCharAt(buffer.length - 1)
        }
        return buffer.toString()
    }

}